package ${package}.${moduleName}.controller;

#if($queryList)
import cn.hutool.core.util.StrUtil;
#end
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.finesys.common.core.constants.RestApiConfig;
import org.finesys.common.core.module.R;
import org.finesys.common.log.core.annotation.LogAnnotation;
import org.finesys.common.log.core.enums.OperateType;
#if($opensource)
import org.finesys.common.excel.annotaion.RequestExcel;
import org.finesys.common.excel.annotaion.ResponseExcel;
#else
import ${package}.common.excel.annotation.ResponseExcel;
import ${package}.common.excel.annotation.RequestExcel;
#end
import ${package}.${moduleName}.entity.${ClassName}Entity;
import ${package}.${moduleName}.entity.${ChildClassName}Entity;
import ${package}.${moduleName}.service.${ClassName}Service;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
#if($isSpringBoot3)
import ${package}.common.security.annotation.HasPermission;
import org.springdoc.core.annotations.ParameterObject;
#else
import org.springframework.security.access.prepost.PreAuthorize;
import org.springdoc.api.annotations.ParameterObject;
#end
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * ${tableComment}
 *
 * @author ${author}
 * @date ${datetime}
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/${functionName}" )
@Tag(description = "${functionName}" , name = "${tableComment}管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ${ClassName}Controller {

    private final  ${ClassName}Service ${className}Service;

    /**
     * 分页查询
     * @param page 分页对象
     * @param ${className} ${tableComment}
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping(RestApiConfig.PAGE)
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_view")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_view')" )
    #end
    public R get${ClassName}Page(@ParameterObject Page page, @ParameterObject ${ClassName}Entity ${className}) {
		 return R.sucess(${className}Service.findPage(page, ${className}));
    }


    /**
     * 通过条件查询${tableComment}
     * @param ${className} 查询条件
     * @return R  对象列表
     */
    @Operation(summary = "通过条件查询" , description = "通过条件查询对象" )
    @GetMapping(RestApiConfig.DETAILS)
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_view")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_view')" )
    #end
    public R getDetails(@ParameterObject ${ClassName}Entity ${className}) {
        return R.sucess(${className}Service.listDeep(Wrappers.query(${className})));
    }

    /**
     * 新增${tableComment}
     * @param ${className} ${tableComment}
     * @return R
     */
    @Operation(summary = "新增${tableComment}" , description = "新增${tableComment}" )
    @LogAnnotation(title = "新增${tableComment}", operateType = OperateType.INSERT)
    @PostMapping
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_add")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_add')" )
    #end
    public R save(@RequestBody ${ClassName}Entity ${className}) {
        return R.sucess(${className}Service.saveDeep(${className}));
    }

    /**
     * 修改${tableComment}
     * @param ${className} ${tableComment}
     * @return R
     */
    @Operation(summary = "修改${tableComment}" , description = "修改${tableComment}" )
    @LogAnnotation(title = "修改${tableComment}", operateType = OperateType.UPDATE)
    @PutMapping
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_edit")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_edit')" )
    #end
    public R updateById(@RequestBody ${ClassName}Entity ${className}) {
        return R.sucess(${className}Service.updateDeep(${className}));
    }

    /**
     * 通过id删除${tableComment}
     * @param ids ${pk.attrName}列表
     * @return R
     */
    @Operation(summary = "通过id删除${tableComment}" , description = "通过id删除${tableComment}" )
    @LogAnnotation(title = "删除${tableComment}", operateType = OperateType.DELETE)
    @DeleteMapping
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_del")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_del')" )
    #end
    public R removeById(@RequestBody ${pk.attrType}[] ids) {
        return R.sucess(${className}Service.removeDeep(ids));
    }

    /**
     * 通过id删除${tableComment}子表数据
     * @param ids ${pk.attrName}列表
     * @return R
     */
    @Operation(summary = "通过id删除${tableComment}子表数据" , description = "通过id删除${tableComment}子表数据" )
	@LogAnnotation(title = "通过id删除${tableComment}子表数据", operateType = OperateType.DELETE)
    @DeleteMapping("/child")
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_del")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_del')" )
    #end
    public R removeChild(@RequestBody ${pk.attrType}[] ids) {
        return R.sucess(${className}Service.removeChild(ids));
    }

    /**
     * 导出excel 表格
     * @param ${className} 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping(RestApiConfig.EXPORT_EXCEL)
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_export")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
    #end
    public List<${ClassName}Entity> exportExcel(${ClassName}Entity ${className},${pk.attrType}[] ids) {
        return ${className}Service.list(Wrappers.lambdaQuery(${className}).in(ArrayUtil.isNotEmpty(ids), ${ClassName}Entity::$str.getProperty($pk.attrName), ids));
    }

    /**
     * 导入excel 表
     * @param ${className}List 对象实体列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
	@PostMapping(RestApiConfig.IMPORT_EXCEL)
    #if($isSpringBoot3)
    @HasPermission("${moduleName}_${functionName}_export")
    #else
    @PreAuthorize("@pms.hasPermission('${moduleName}_${functionName}_export')" )
    #end
    public R importExcel(@RequestExcel List<${ClassName}Entity> ${className}List, BindingResult bindingResult) {
        return R.sucess(${className}Service.saveBatch(${className}List));
    }
}
